//
//  LDBaseManyTagsView.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/5/26.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

class LDBaseManyTagsView: UIView {
    
    var SameLabTag = 888
    var sendParamsColsure: ((index:Int)->Void)?
    var totalLabHeight:CGFloat = 25
    var addHightBtn:UIButton?
    internal var limitTags = 0
    
    var titleParams = [String]() {
        
        didSet{
            
            performSelectorOnMainThread(#selector(setup), withObject: self, waitUntilDone: true)
        }
    }
    
    var tapcosure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = LDGolbalColor.White
    }
    
    func setup() {
        
        self.setDefaultContent()
    }
    
    
    
    func setDefaultContent() {
        for item in self.subviews {
            item.removeFromSuperview()
        }
        frame = LDFrame.ScreenBounds
        var newtitleParams:Array<String> = titleParams
        
        let count:Int = newtitleParams.count
        
        let labV_Margin = Margin_V*5
        var labX:CGFloat = Margin_V*5
        var labY:CGFloat = Margin_V*3
        var totalW:CGFloat = Margin_V*4
        
        let labH:CGFloat = 30
        
        for i in 0..<count {
            
            let lab = UILabel.setDetailsLab(newtitleParams[i], fontSize: 13, titleColor: LDFontColor.DeepGray, Alignment: .Center)
            
            addSubview(lab!)
            
            lab?.backgroundColor = LDGolbalColor.White
            lab?.layer.cornerRadius = 3
            lab?.layer.borderWidth = 1
            lab?.layer.borderColor = LDIconColor.LightGray.CGColor
            lab?.userInteractionEnabled = true
            
            lab?.enabled = false
            lab?.tag = SameLabTag + i
            
            lab?.bk_whenTapped({ [weak self] in
                self?.sendNoti(i, tagTitle: newtitleParams[i], control: "nil", lab: lab!)
                })
            
            let rect = NSString.init(string: newtitleParams[i]).boundingRectWithSize(CGSizeMake(300, 13), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font13], context: nil)
            
            let labW = rect.width + LDMargin_H*2
            totalW += labW + LDMargin_H*2
            
            if totalW > (self.width) {
                totalW = Margin_H*4 + labW
                labX = labV_Margin
                labY = labY + labH + LDMargin_V*3
            }
            
            lab!.frame = CGRectMake(labX, labY, labW, labH)
            labX = labX + labW + LDMargin_H*2
            
            totalLabHeight = CGRectGetMaxY(lab!.frame)
            
        }
        
    }
    //
    func sendTapAddHightLightBtn(closure:(()->Void)?) {
        
        self.tapcosure = closure
    }
    
    //通知 n控制器
    func sendNoti(index:Int,tagTitle:String,control:String,lab:UILabel) {
        

        if sendParamsColsure != nil {
            sendParamsColsure!(index: index)
        }
    }
    
    func sendTapTag(colsure:((index:Int)->Void)) {
        sendParamsColsure = colsure
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
